<?php


namespace app\service\admin;

use laytp\library\Token;
use laytp\traits\Error;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\SimpleType\Jc;

/**
 * 后台用户服务实现者
 * @package app\service\admin
 */
class GenerateWord
{

    private $phpWord;
    public function init()
    {

    }

    private function examConfig()
    {
        $config = [
            1       => "单选题（共15题，每题2分）",
            2       => "多选题（共15题，每题2分）",
            3       => "判断题（共10题，每题2分）",
            4       => "简答题（共2题，每题10分）",
        ];
        return $config;
    }

    public function createWord($data, $paperName = '')
    {
        $examConfig = $this->examConfig();
        $phpWord = new PhpWord();

        // 添加一个新的空白页面A4
        $pageWidth = 297;
        $pageHeight = 210;
        //'orientation' => 'landscape'纵向
        $section = $phpWord->addSection(['pageWidth' => $pageWidth * 1000, 'pageHeight' => $pageHeight * 1000, 'orientation' => null]);

        $section->addText('秘密★10年', ['name' => '仿宋', 'size' => 16, 'bold' => false, 'color' => 'red']);
        $section->addText($paperName, ['name' => '方正小标宋简体', 'size' => 22,  'bold' => true], ['alignment' => Jc::CENTER,]);

        //单选题
        $setting = ['indentation' => ['firstLine' => 500]];
        $section->addText($examConfig[1], ['name' => '黑体', 'size' => 16, 'bold' => true]);
        foreach ($data[1] as $key => $val) {
            $key++;
            $section->addText($key.". ".$val['title']. "（   ）", ['name' => '仿宋', 'size' => 16, 'bold' => false]);
            $section->addText('A. ' . $val['option_a'], ['name' => '仿宋', 'size' => 16, 'bold' => false], $setting);
            $section->addText('B. ' . $val['option_b'], ['name' => '仿宋', 'size' => 16, 'bold' => false], $setting);
            $section->addText('C. ' . $val['option_c'], ['name' => '仿宋', 'size' => 16, 'bold' => false], $setting);
            $section->addText('D. ' . $val['option_d'], ['name' => '仿宋', 'size' => 16, 'bold' => false], $setting);

            if (empty($val['option_e'])) continue;

        }
        #多选
        $section->addText($examConfig[2], ['name' => '黑体', 'size' => 16, 'bold' => true]);
        foreach ($data[2] as $key => $val) {

            $key++;
            $section->addText($key .". ".$val['title']. "（   ）", ['name' => '仿宋', 'size' => 16, 'bold' => false]);
            $section->addText('A. ' . $val['option_a'], ['name' => '仿宋', 'size' => 16, 'bold' => false], $setting);
            $section->addText('B. ' . $val['option_b'], ['name' => '仿宋', 'size' => 16, 'bold' => false], $setting);
            $section->addText('C. ' . $val['option_c'], ['name' => '仿宋', 'size' => 16, 'bold' => false], $setting);
            $section->addText('D. ' . $val['option_d'], ['name' => '仿宋', 'size' => 16, 'bold' => false], $setting);
            $section->addText('E. ' . $val['option_e'], ['name' => '仿宋', 'size' => 16, 'bold' => false], $setting);
            $section->addText('F. ' . $val['option_f'], ['name' => '仿宋', 'size' => 16, 'bold' => false], $setting);

            if (empty($val['option_e'])) continue;

        }
        #判断
        $section->addText($examConfig[3], ['name' => '黑体', 'size' => 16, 'bold' => true]);
        foreach ($data[3] as $key => $val) {
            $key++;
            $section->addText($key.". ".$val['title']. "（   ）", ['name' => '仿宋', 'size' => 16, 'bold' => false]);

        }
        #简答
        $section->addText($examConfig[4], ['name' => '黑体', 'size' => 16, 'bold' => true]);
        foreach ($data[4] as $key => $val) {
            $key++;
            $section->addText($key.". ".$val['title'], ['name' => '仿宋', 'size' => 16, 'bold' => false]);
            for($i = 1; $i <= 10; $i++) {
                $section->addText('');
            }

        }


        $section->addText(''); //添加一个空行
        $section->addText('***请在打印时适当调整格式***', ['bold' => true, 'size' => 16, 'color' => 'red']);

        // 保存文档
        $xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        $url = "/upload/exam/{$paperName}". date('YmdHis') . ".docx";
        $xmlWriter->save("/www/wwwroot/ly_exam/public/" . $url);

        return $url;
    }
}
